package com.honey.mybatis.generator.dao.method;

import com.honey.compilation.java.AbstractJavaCompilation;
import com.honey.compilation.java.IfBlock;
import com.honey.compilation.java.JavaKeyWord;
import com.honey.compilation.java.Method;
import com.honey.core.builder.CharacterBuilder;
import com.honey.core.builder.CharacterBuilderFactory;
import com.honey.core.dbmapping.introspect.IntrospectSchema;
import com.honey.core.dbmapping.introspect.IntrospectSchemaColumn;
import com.honey.core.types.FullyQualifiedJavaType;
import com.honey.mybatis.generator.MybatisPlugin;
import com.honey.mybatis.generator.dao.DaoConstant;
import com.honey.mybatis.generator.page.PageConstant;

/**
 * 按照selectAll查询
 * @author Administrator
 *
 */
public class SelectAllMethod  extends AbstractCreateInterfaceMethod{

	public static final String METHOD_NAME="selectAll";
	
	private boolean isUseSession = false ;

	private boolean isPage = false ;
		
	public SelectAllMethod(
			AbstractJavaCompilation clazz, MybatisPlugin belongPlugin,
			IntrospectSchema introspectSchema,
			IntrospectSchemaColumn introspectSchemaColumn
			) {
		super(clazz, belongPlugin, introspectSchema, introspectSchemaColumn);
	}
	
	public SelectAllMethod(
			AbstractJavaCompilation clazz, MybatisPlugin belongPlugin,
			IntrospectSchema introspectSchema,
			IntrospectSchemaColumn introspectSchemaColumn,
			boolean  isUseSession,
			boolean  isPage
			) {
		super(clazz, belongPlugin, introspectSchema, introspectSchemaColumn);
		this.isUseSession = isUseSession;
		this.isPage = isPage;
	}
	
	/* (non-Javadoc)
	 * @see com.honey.mybatis.generator.AbstractMethod#addBody(com.honey.compilation.java.Method)
	 */
	@Override
	protected void addBody(Method method) {
		if( isUseSession() ){
			IfBlock ifBlock = new IfBlock(DaoConstant.SQL_SESSION_NAME+" == null");
			ifBlock.addBodyLine("return null");
			method.addBlocks(ifBlock);
		}
		if(isUsePage()){
			addBody2(method);
		}else{
			addBody1(method);
		}
	}

	private void addBody1(Method method){
		method.addBodyLine(
			new StringBuilder()
			.append(method.getReturnType().getFullName())
			.append(" list = ")
			.append(mybatisPlugin.getDependentModel().call(null))
			.append("selectList ( ")
			.append( isUseSession()? DaoConstant.SQL_SESSION_NAME+" , ":""  )
			.append( super.getMapperId().getSelectByDynamicMapperId() )
			.append(" , ")
			.append( super.getMapperId().getSelectCountMapperId() )
			.append(" , null , new " + PageConstant.PAGE_NAME + "(-1,-1,-1) )")
			.toString()
		);
		method.addBodyLine("return list ");
	}
	
	private void addBody2(Method method){
		CharacterBuilder cb = CharacterBuilderFactory.createDefaultStringBuilder();
		cb.append(JavaKeyWord.RETURN)
		.append("selectByDynamic(")
		.append( isUseSession()? DaoConstant.SQL_SESSION_NAME+", ":"" )
		.append("null,")
		.append(PageConstant.PAGE_VARIABLE_NAME)
		.append(")");
		method.addBodyLine(cb.toString());
	}
	
	/*
	 * (non-Javadoc)
	 * @see com.honey.mybatis.generator.AbstractMethod#getMethodName()
	 */
	@Override
	protected String getMethodName() {		
		String answer = null;
		if( !isUseSession ){
			answer = METHOD_NAME;
		}else if( isUseSession  && mybatisPlugin.isEnableUseSqlSession() ){
			answer = METHOD_NAME;
		}
		return answer;
	}
	
	/**
	 * @return the isUseSession
	 */
	@Override
	public final boolean isUseSession() {
		return isUseSession;
	}

	/* (non-Javadoc)
	 * @see com.honey.mybatis.generator.AbstractMethod#getJavaDoc()
	 */
	@Override
	protected String[] getJavaDoc() {
		String[] answer = new String[]{
			"查询所有的记录。。"
		};
		return answer;
	}

	/* (non-Javadoc)
	 * @see com.honey.mybatis.generator.AbstractMethod#getReturnType()
	 */
	@Override
	protected FullyQualifiedJavaType getReturnType() {
		return new FullyQualifiedJavaType("java.util.List<"+ super.getEntityClassName(introspectSchema.getSchema()) +">");
	}

	
	/* (non-Javadoc)
	 * @see com.honey.mybatis.generator.dao.method.AbstractCreateInterfaceMethod#isUsePage()
	 */
	@Override
	public boolean isUsePage() {
		return this.isPage;
	}

	/* (non-Javadoc)
	 * @see com.honey.mybatis.generator.dao.method.AbstractCreateInterfaceMethod#isCondition()
	 */
	@Override
	public boolean isCondition() {		
		return false;
	}
}
